package cn.zhuziym.nvw;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @Title: DownThread.java
 * @Package javatest.nvw
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 作者 grq
 * @version 创建时间：2018年8月26日 上午2:12:48
 *
 */
public class DownThread extends Thread {

	private String indexUrl;
	private String save;

	public DownThread(String _indexUrl) {
		this.indexUrl = _indexUrl;
		StringBuffer sBuffer = new StringBuffer(indexUrl);
		sBuffer.deleteCharAt(sBuffer.length() - 1);
		this.save = sBuffer.toString().substring(sBuffer.lastIndexOf("/"));

	}

	@Override
	public void run() {
		Document url = Utils.getUrl(indexUrl);
		Element picNum = url.getElementById(Constant.picNumId);
		String numStr = picNum.getElementsByTag("span").text().split("张")[0];
		int num = Integer.valueOf(numStr);
		getPics(url);
		for (int i = 2; i < (num + 2) / 3; i++) {
			getPics(Utils.getUrl(indexUrl + i + ".html"));
		}

	}

	private void getPics(Document url) {
		Elements deailPics = url.getElementById(Constant.DETAIL_PICS_ID_STRING).getElementsByTag("img");
		for (Element element : deailPics) {
			Utils.downFile(element.attr("src"), this.save);
			// System.out.println(
			// this.save+"--------------下载--"+element.attr("src"));
		}
	}

}
